// pages/classgoods/classgoods.js
let myRequest = require('../../config/myRequest.js');
let api = require('../../config/api.js');
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    domain: api.domain,
    navList: [],
    goodsList: [],
    cartlist: [],
    nav_id: null,
    scrollStatus: true, //下拉加载是否有数据
    isEnd: false,
    noData: true,
    showModal: false,
    cat_id: '',
    cat_sort: '',
    scrollTop: 0,
    keyword: ''
  },
  // 获取表单输入内容
  changeInputContent(e) {
    const that = this;
    that.setData({
      keyword: e.detail.value
    })
  },
  // 提交搜索
  searchSubmit() {
    const that = this;
    that.setData({
      goodsList: [],
      scrollStatus: true,
      scrollTop: 0,
      page: 1,
      isEnd: false,
      noData: true
    })
    that.setData({
      hiddenLoading: false
    })
    myRequest.EasyHttp.post(api.reqUrls.getGoodsList, {
      token: app.globalData.token,
      page: that.data.page,
      cat_id1: that.data.nav_id,
      goods_name: that.data.keyword
    }).then((res) => {
      that.setData({
        hiddenLoading: true
      })
      if (res.data.status == 1) {
        let noData = true;
        if (res.data.result.length == 0) {
          noData = false
        } else {
          noData = true
        }
        that.setData({
          goodsList: res.data.result,
          noData
        })
      }
    })

  },
  //获取选项卡相对应的商品列表
  bindNav(e) {
    const that = this;
    var nav_id = '';
    if (typeof e == 'string') {
      nav_id = that.data.cat_id;
    } else {
      nav_id = e.currentTarget.dataset.nav_id;
    }
    that.setData({
      nav_id: nav_id,
      goodsList: [],
      scrollStatus: true,
      scrollTop: 0,
      page: 1,
      isEnd: false,
      noData: true
    })
    that.setData({
      hiddenLoading: false
    })
    myRequest.EasyHttp.post(api.reqUrls.getGoodsList, {
      token: app.globalData.token,
      page: that.data.page,
      cat_id1: that.data.nav_id,
      goods_name: that.data.keyword
    }).then((res) => {
      that.setData({
        hiddenLoading: true
      })
      if (res.data.status == 1) {
        let noData = true;
        if (res.data.result.length == 0) {
          noData = false
        } else {
          noData = true
        }
        that.setData({
          goodsList: res.data.result,
          noData
        })
      }
    })
  },
  //添加
  transmit() {
    const that = this;
    console.log("转发事件")
    wx.navigateTo({
      url: '/pages/addGoods/addGoods',
    })
  },
  //下架
  eduitxiajia(e) {
    console.log('this is 下架');
    const that = this;
    wx.showModal({
      content: e.currentTarget.dataset.goods_sale ? '是否下架该商品' : '是否上架该商品',
      showCancel: true,
      confirmText: "确定",
      confirmColor: "#eb3223",
      success(res) {
        if (res.confirm) {
          that.setData({
            hiddenLoading: false
          })
          myRequest.EasyHttp.post(api.reqUrls.getonlineOrOffline, {
            token: app.globalData.token,
            goods_id: e.currentTarget.dataset.goods_id,

          }).then(res => {
            that.setData({
              hiddenLoading: true
            })
            if (res.data.status == 1) {
              const goodslist = that.data.goodsList;
              goodslist.map(i => {
                if (i.goods_id == e.currentTarget.dataset.goods_id) {
                  i.is_on_sale = !e.currentTarget.dataset.goods_sale;
                }
              })
              that.setData({
                goodsList: goodslist
              })
            } else {
              wx.showToast({
                title: res.data.msg,
                icon: 'none',
              })
            }
          })
        }
      },
    })
  },
  //编辑
  eduitinfo(e) {
    const that = this;
    that.popup.changeRange(e);
  },
  showTab: function (e) {
    console.log('this is showtabBar');
    console.log(e.detail);
    const that = this;
    that.setData({
      hiddenLoading: false
    })
    myRequest.EasyHttp.post(api.reqUrls.getupdate, {
      token: app.globalData.token,
      goods_id: e.detail.goods_id,
      goods_name: e.detail.goods_name,
      shop_price: e.detail.prince,
      store_count: e.detail.nums,
      store_cat_id1: e.detail.store_cat_id1,
      store_cat_id2: e.detail.store_cat_id2,
      weight: e.detail.goods_weight,
      sort: e.detail.sort,
    }).then(res => {
      that.setData({
        hiddenLoading: true
      })
      if (res.data.status == 1) {

        if (e.detail.isreload == true) {
          //修改了排序，需要刷新
          that.setData({
            cat_id: e.detail.store_cat_id1
          })
          that.bindNav(e.detail.store_cat_id1);

        } else if (e.detail.isdell == true) {
          //修改了分类，需要删除
          const goodslist = that.data.goodsList;
          goodslist.map(i => {
            if (i.goods_id == e.detail.goods_id) {
              var index = goodslist.indexOf(i);
              goodslist.splice(index, 1);
            }
          })
          that.setData({
            goodsList: goodslist
          })
        } else {
          //修改数据
          const goodslist = that.data.goodsList;
          goodslist.map(i => {
            if (i.goods_id == e.detail.goods_id) {
              i.goods_name = e.detail.goods_name,
                i.weight = e.detail.goods_weight,
                i.shop_price = e.detail.prince,
                i.store_count = e.detail.nums
            }
          })
          that.setData({
            goodsList: goodslist
          })
        }

      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
        })
      }
    })
  },
  //推荐
  tuijianinfo(e){
    console.log('this is 推荐');
    const that = this;
    wx.showModal({
      content: e.currentTarget.dataset.goods_sale ? '是否取消推荐该商品' : '是否推荐该商品',
      showCancel: true,
      confirmText: "确定",
      confirmColor: "#eb3223",
      success(res) {
        if (res.confirm) {
          that.setData({
            hiddenLoading: false
          })
          myRequest.EasyHttp.post(api.reqUrls.setGoodsRecommend, {
            token: app.globalData.token,
            goods_id: e.currentTarget.dataset.goods_id,
            is_recommend: !e.currentTarget.dataset.goods_sale ? '1' : '0'
          }).then(res => {
            that.setData({
              hiddenLoading: true
            })
            if (res.data.status == 1) {
              const goodslist = that.data.goodsList;
              goodslist.map(i => {
                if (i.goods_id == e.currentTarget.dataset.goods_id) {
                  i.is_recommend = !e.currentTarget.dataset.goods_sale;
                }
              })
              that.setData({
                goodsList: goodslist
              })
            } else {
              wx.showToast({
                title: res.data.msg,
                icon: 'none',
              })
            }
          })
        }
      },
    })
  },
  //修改分类排序
  bindupdateClassSort(e) {
    console.log(e, 77777);
    this.setData({
      showModal: true,
      cat_sort: e.currentTarget.dataset.cat_sort,
      cat_id: e.currentTarget.dataset.cat_id
    })

  },
  /**
   * 弹出框蒙层截断touchmove事件
   */
  preventTouchMove: function () {},
  /**
   * 隐藏模态对话框
   */
  hideModal: function () {
    this.setData({
      showModal: false
    });
  },
  /**
   * 对话框取消按钮点击事件
   */
  onCancel: function () {
    this.hideModal();
    this.setData({
      cat_id: '',
      cat_sort: '',
    });
  },
  /**
   * 对话框确认按钮点击事件
   */
  onConfirm: function () {
    const that = this;
    this.hideModal();

    wx.showModal({
      content: '是否修改分类排序',
      showCancel: true,
      confirmText: "确定",
      confirmColor: "#eb3223",
      success(res) {
        if (res.confirm) {
          that.setData({
            hiddenLoading: false
          })
          myRequest.EasyHttp.post(api.reqUrls.getupdateClassSort, {
            token: app.globalData.token,
            cat_id: that.data.cat_id,
            cat_sort: that.data.cat_sort,

          }).then(res => {
            that.setData({
              hiddenLoading: true
            })
            if (res.data.status == 1) {
              return myRequest.EasyHttp.post(api.reqUrls.getGoodsClass, {
                token: app.globalData.token,
              })

            } else {
              wx.showToast({
                title: res.data.msg,
                icon: 'none',
              })
            }
          }).then((res) => {
            console.log(res)
            if (res.data.status == 1) {
              that.setData({
                hiddenLoading: true
              })
              that.setData({
                navList: res.data.result,
              })
            }
          })
        }
      },
    })


  },
  //添加清洁方式
  inputChange(e) {
    const that = this;
    that.setData({
      cat_sort: e.detail.value
    })
  },

  //上拉加载
  scrollBottom(e) {
    console.log(222222)
    const that = this;
    if (that.data.scrollStatus) {
      that.setData({
        hiddenLoading: false
      })
      let pages = that.data.page;
      pages++;
      myRequest.EasyHttp.post(api.reqUrls.getGoodsList, {
        page: pages,
        token: app.globalData.token,
        cat_id1: that.data.nav_id,
        goods_name: that.data.keyword
      }).then(res => {
        that.setData({
          hiddenLoading: true
        })
        if (res.data.status == 1) {
          if (res.data.result == '') {
            that.setData({
              scrollStatus: false,
              isEnd: true,
              hiddenLoading: true
            })
          } else {
            that.setData({
              goodsList: [...that.data.goodsList, ...res.data.result],
              page: pages
            })
          }
        }
      })
    }
  },
  // 获取购物车数据
  getCartList() {

  },
  // 修改商品数量
  myevents(e) {
    const that = this;
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    console.log(that.data.domain, 55555)
    that.setData({
      hiddenLoading: false
    })
    myRequest.EasyHttp.post(api.reqUrls.getGoodsClass, {
      token: app.globalData.token,
    }).then(res => {
      console.log(res)
      if (res.data.status == 1) {
        that.setData({
          navList: res.data.result,
          nav_id: res.data.result[0].cat_id
        })
        return myRequest.EasyHttp.post(api.reqUrls.getGoodsList, {
          token: app.globalData.token,
          page: that.data.page,
          cat_id1: that.data.nav_id,
          goods_name: that.data.keyword
        })
      } else {
        that.setData({
          hiddenLoading: false
        })
      }
    }).then((res) => {
      console.log(res, 4444)
      if (res.data.status == 1) {
        that.setData({
          hiddenLoading: true
        })
        let noData = true;
        if (res.data.result.length == 0) {
          noData = false
        } else {
          noData = true
        }
        that.setData({
          goodsList: res.data.result,
          noData
        })
      } else {
        that.setData({
          hiddenLoading: true
        })
      }
    })
  },

  getAllData() {
    const that = this;
    that.setData({
      hiddenLoading: false
    })
    myRequest.EasyHttp.post(api.reqUrls.getGoodsClass, {
      token: app.globalData.token,
    }).then(res => {
      if (res.data.status == 1) {
        that.setData({
          navList: res.data.result,
          nav_id: app.globalData.cat_id || res.data.result[0].cat_id,
          page: 1,
          scrollStatus: true,
          scrollTop: 0,
          isEnd: false,
          goodsList: [],
          noData: true
        })
        that.setData({
          hiddenLoading: true
        })
        return myRequest.EasyHttp.post(api.reqUrls.getGoodsList, {
          token: app.globalData.token,
          page: that.data.page,
          cat_id1: that.data.nav_id,
          goods_name: that.data.keyword
        })
      }

    }).then((res) => {
      if (res.data.status == 1) {
        that.setData({
          hiddenLoading: true
        })
        let noData = true;
        if (res.data.result.length == 0) {
          noData = false
        } else {
          noData = true
        }
        that.setData({
          goodsList: res.data.result,
          noData
        })
        app.globalData.tabColonel = false;
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.popup = this.selectComponent("#popup");
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const that = this;
     that.getAllData();
  },
  changeData: function () {
    const that = this;
    console.log(333333333)
    that.getAllData(); //最好是只写需要刷新的区域的代码，onload也可，效率低，有点low

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    app.globalData.cat_id = null;
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    const that = this;
    that.setData({
      hiddenLoading: false,
      scrollStatus: true,
      scrollTop: 0,
      goodsList: [],
      isEnd: false,
      page: 1,
      noData: true
    })
    myRequest.EasyHttp.post(api.reqUrls.getGoodsClass, {
      token: app.globalData.token,
    }).then(res => {
      if (res.data.status == 1) {
        that.setData({
          navList: res.data.result,
          nav_id: res.data.result[0].cat_id
        })
        return myRequest.EasyHttp.post(api.reqUrls.getGoodsList, {
          token: app.globalData.token,
          page: that.data.page,
          cat_id1: that.data.nav_id,
          goods_name: that.data.keyword
        })
      }

    }).then((res) => {
      if (res.data.status == 1) {
        that.setData({
          hiddenLoading: true
        })
        let noData = true;
        if (res.data.length == 0) {
          noData = false
        } else {
          noData = true
        }
        wx.stopPullDownRefresh()
        that.setData({
          goodsList: res.data.result,
          noData
        })
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})